//
//  CABasicAnimationMakeUpClockViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/26.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "CABasicAnimationMakeUpClockViewController.h"

@interface CABasicAnimationMakeUpClockViewController ()<CAAnimationDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *horologeImageV;
@property (weak, nonatomic) IBOutlet UIImageView *hourImageV;
@property (weak, nonatomic) IBOutlet UIImageView *minuteImageV;
@property (weak, nonatomic) IBOutlet UIImageView *secondImageV;
@property (nonatomic, strong) NSTimer *time;
@end

@implementation CABasicAnimationMakeUpClockViewController

/**
 * 温馨提示：我这个时钟画的有点丑，并且经过测试我发现秒针有时候会出现晃动的情况，这个不是程序本身的问题，而是图片的问题(因为图片不标准，并且在xib中我是大致放了一下)
 */

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.time invalidate];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
        self.view.backgroundColor = [UIColor whiteColor];
    
    // 改变锚点
    self.hourImageV.layer.anchorPoint = CGPointMake(0.5, 0.9);
    self.minuteImageV.layer.anchorPoint = CGPointMake(0.5, 0.9);
    self.secondImageV.layer.anchorPoint = CGPointMake(0.5, 0.9);
    

    self.time = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(goTime:) userInfo:nil repeats:YES];
    [self.time fire];
    
    self.horologeImageV.image = [UIImage imageWithName:@"horologe"];
    self.hourImageV.image = [UIImage imageWithName:@"hour"];
    self.minuteImageV.image = [UIImage imageWithName:@"minute"];
    self.secondImageV.image = [UIImage imageWithName:@"second"];
    
    // 一开始的时候让时间设置正确
    [self updatePointImagesAnimation:NO];
}

- (void) goTime:(NSTimer *)timer
{
    [self updatePointImagesAnimation:YES];
}

- (void)updatePointImagesAnimation:(BOOL)animated
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSUInteger units = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
   // NSLog(@"小时：%f，分钟：%f， 秒：%f", components.hour / 12.0, components.minute / 60.0, components.second / 60.0);
    CGFloat hoursAngle = (components.hour / 12.0) * M_PI * 2.0;
    CGFloat minuteAngel = (components.minute / 60.0) * M_PI * 2.0;
    CGFloat secondAngel = (components.second / 60.0) * M_PI * 2.0;
    
    [self setAngle:hoursAngle forPointImage:self.hourImageV animation:animated];
    [self setAngle:minuteAngel forPointImage:self.minuteImageV animation:animated];
    [self setAngle:secondAngel forPointImage:self.secondImageV animation:animated];
}

// 运用动画设置指针的角度
- (void)setAngle:(CGFloat)angle forPointImage:(UIView *)pointView animation:(BOOL)animated
{
    CATransform3D transform = CATransform3DMakeRotation(angle, 0, 0, 1);

    if (animated) {
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
   //     animation.fromValue = [pointView.layer.presentationLayer valueForKey:@"transform"];
        animation.toValue = [NSValue valueWithCATransform3D:transform];
        animation.duration = 0.5;
        animation.delegate = self;
        [animation setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:1 :0 :0.75 :1]]; // 先慢后快，这样更符合时钟的规律
        [animation setValue:pointView forKey:@"pointView"];
        [pointView.layer addAnimation:animation forKey:@"changeAngle"];
        
    } else {
        pointView.layer.transform = transform;
    }
}

- (void)animationDidStop:(CABasicAnimation *)anim finished:(BOOL)flag
{
    UIView *handView = [anim valueForKey:@"pointView"];

    // 防止回去
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    [CATransaction setAnimationDuration:0];
     handView.layer.transform = [anim.toValue CATransform3DValue];
    [CATransaction commit];
}

@end
